package org.mindrive.system.dao;

import java.util.List;
import java.util.stream.Collectors;

import org.mindrive.system.collection.system.PermissionEntity;
import org.mindrive.system.collection.system.PermissionPathEntity;
import org.mindrive.system.dao.mapper.PermissionMapper;
import org.mindrive.system.dao.mapper.PermissionPathMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author houwei
 * @since 2021-10-14
 */
@Service
public class PermissionDao extends ServiceImpl<PermissionMapper, PermissionEntity> {

	@Autowired
	private PermissionPathMapper permissionPathMapper;

	@Autowired
	private PermissionMapper permissionMapper;

	public void createPath(String parentId, String id) {
		PermissionPathEntity path = new PermissionPathEntity();
		path.setAncestor(id);
		path.setDescendant(id);
		path.setDistance(0);
		permissionPathMapper.insert(path);

		QueryWrapper<PermissionPathEntity> query = new QueryWrapper<PermissionPathEntity>();
		query.eq("descendant", parentId);
		query.orderByDesc("distance");
		List<PermissionPathEntity> ancestors = permissionPathMapper.selectList(query);
		for (PermissionPathEntity ancestor : ancestors) {
			PermissionPathEntity pathAncestor = new PermissionPathEntity();
			pathAncestor.setAncestor(ancestor.getAncestor());
			pathAncestor.setDescendant(id);
			pathAncestor.setDistance(ancestor.getDistance() + 1);
			permissionPathMapper.insert(pathAncestor);
		}

	}

	public List<String> getDescendants(String id) {

		QueryWrapper<PermissionPathEntity> query = new QueryWrapper<PermissionPathEntity>();
		query.eq("ancestor", id).ne("distance", 0).orderByAsc("distance");
		List<PermissionPathEntity> children = permissionPathMapper.selectList(query);
		List<String> descendants = children.stream().map(item -> item.getDescendant()).collect(Collectors.toList());
		return descendants;

	}

	public List<String> getAncestors(String id) {
		QueryWrapper<PermissionPathEntity> query = new QueryWrapper<PermissionPathEntity>();
		query.eq("descendant", id).ne("distance", 0).orderByDesc("distance");
		List<PermissionPathEntity> ancestors = permissionPathMapper.selectList(query);
		List<String> ancestorIds = ancestors.stream().map(item -> item.getAncestor()).collect(Collectors.toList());
		return ancestorIds;
	}

	public List<PermissionEntity> search(List<String> permissionIds, String menuType) {
		QueryWrapper<PermissionEntity> query = new QueryWrapper<PermissionEntity>();
		query.in("id", permissionIds).eq("menu_type", menuType);
		query.orderByAsc("sort_no");
		List<PermissionEntity> list = permissionMapper.selectList(query);
		return list;

	}

	public List<PermissionEntity> search(String menuType) {
		QueryWrapper<PermissionEntity> query = new QueryWrapper<PermissionEntity>();
		query.eq("menu_type", menuType);
		query.orderByAsc("sort_no");
		List<PermissionEntity> list = permissionMapper.selectList(query);
		return list;
	}

	public PermissionEntity getPermission(String menuType, String permissionCode) {
		QueryWrapper<PermissionEntity> query = new QueryWrapper<PermissionEntity>();
		query.eq("menu_type", menuType).eq("permission_code", permissionCode);
		PermissionEntity entity = permissionMapper.selectOne(query);
		return entity;
	}

}
